import QtQuick 2.12
import QtQuick.Controls 2.5
import QtQuick.Window 2.12
import QtQuick.Layouts 1.12
import QtQuick.Dialogs 1.0
import AIClient.module 1.0
Page {
    signal grantDataset(string uid, string email, int role, int from)
    signal cancel(int from)
    signal error(string msg)

    AIClient {
        id: client
    }

    id: dataset_grant
    title: "数据集授权"
    visible: false

    width: parent.width*0.8
    height: parent.height*0.8

    property string m_uid: ""
    property string m_dataset_name: ""
    property int m_from: 0

    Binding {
        target: dataset_grant
        property: "m_dataset_name"
        value: dataset_name.text
    }
    Binding {
        target: dataset_name
        property: "text"
        value: dataset_grant.m_dataset_name
    }

    function clear()
    {
        email.text = ""
        roleCombo.currentIndex = -1
    }

    ListModel {
        /* 数据项 */
        id: roleModel
        ListElement {
            role: 0;
            text: "创建者";
            color: "black";
        }
        ListElement {
            role: 0;
            text: "管理者";
            color: "black";
        }
        ListElement {
            role: 1;
            text: "编辑者";
            color: "black";
        }
        ListElement {
            role: 2;
            text: "使用者";
            color: "black";
        }
    }

    Rectangle {
        anchors.centerIn: parent
        color:"azure"
        width: dataset_grant.width
        height: dataset_grant.height

        GridLayout {
//            ScrollBar.vertical: ScrollBar {
//                id: scrollBar
//                parent: flickable.parent
//                anchors.top: flickable.top
//                anchors.left: flickable.right
//                anchors.bottom: flickable.bottom
//            }

            id: grid_dataset_grant
            rows: 5
            columns: 2
            anchors.topMargin: 100
            anchors.bottomMargin: 100
            anchors.leftMargin: 100
            anchors.rightMargin: 100
            anchors.fill: parent
            columnSpacing: 5
            rowSpacing: 15
            Text {
                text: qsTr("数据集名称")
                font.pointSize: 12
                font.bold: false
                Layout.preferredHeight: 50
                Layout.maximumHeight: 60
//                Layout.fillHeight: true
                Layout.fillWidth: true
                color: "black"
                Layout.column: 0
                Layout.row: 0
            }
            TextField {
                id: dataset_name
                text: m_dataset_name
                selectByMouse: true
                font.pointSize: 12
                Layout.minimumHeight: 12*1.5
                Layout.preferredHeight: 50
                Layout.preferredWidth: 500
                Layout.maximumHeight: 60
//                Layout.fillHeight: true
                Layout.fillWidth: true
                Layout.row: 0
                Layout.column: 1
                enabled: false
            }
            Text {
                text: qsTr("授权用户")
                font.pointSize: 12
                Layout.preferredHeight: 50
                Layout.maximumHeight: 60
//                Layout.fillHeight: true
                Layout.fillWidth: true
                Layout.row: 1
                Layout.column: 0
                color: "black"
            }
            TextField {
                id: email
                text: ""
                selectByMouse: true
                font.pointSize: 12
                Layout.minimumHeight: 12*1.5
                Layout.preferredHeight: 50
                Layout.preferredWidth: 500
                Layout.maximumHeight: 60
//                Layout.fillHeight: true
                Layout.fillWidth: true
                Layout.row: 1
                Layout.column: 1
                property bool m_sel: false
                onTextChanged: {
                    if ((email.focus) && (!m_sel))
                    {
                        email_lst_model.clear()
                        var res = client.search_email(email.text)
                        var jres = JSON.parse(res)
                        if ( jres["code"] === 0 )
                        {
                            var data = jres["data"]["users"]
                            for ( var i=0; i<data.length ; i++ )
                            {
                                var user = {"user":data[i]}

                                email_lst_model.append(user)
                            }
                        }
                        email_lst_view.open()
//                        email_lst_view.visible = true
                        //                        email_lst_view.x = email.left
                        //                        email_lst_view.y = email.bottom
                    }
                    else
                    {
                        email_lst_view.visible = false
//                        email_lst_view.visible = false
                    }
                }
                Popup {
                    id: email_lst_view
                    width: email.width
                    height: 300
                    x: 0
                    y: email.height
                    ListView {

                        model: email_lst_model
                        anchors.fill: parent
                        //visible: false


                        delegate: ItemDelegate {
                            height: 40
                            width: parent.width
                            background: Rectangle{
                                color: hovered?"gray":"white"
                                anchors.fill: parent

                            }

                            text: user
                            onClicked: {
                                email.m_sel = true
                                email.text = user
                                email.m_sel = false
                                email_lst_view.visible = false
                            }
                        }

                    }
                }
                ListModel {
                    id: email_lst_model
                    ListElement {
                        user:"xx@gmail.com"
                    }
                }
            }
            Text {
                text: qsTr("授权角色")
                font.pointSize: 12
                Layout.preferredHeight: 50
                Layout.maximumHeight: 60
//                Layout.fillHeight: true
                Layout.fillWidth: true
                Layout.row: 2
                Layout.column: 0
                color: "black"
            }
            ComboBox {
                id: roleCombo
                currentIndex: -1
                Layout.column: 1
                Layout.row: 2
                font.pointSize: 12
                Layout.minimumHeight: 12*3
                Layout.preferredHeight: 50
                Layout.preferredWidth: 500
                Layout.maximumHeight: 60
//                Layout.fillHeight: true
                Layout.fillWidth: true
                model: roleModel
                textRole: "text"
            }

            RowLayout {
                Layout.row: 3
                Layout.column: 1
                OrangeButton {
                    id: btn
                    text: "授权"
//                    Layout.fillHeight: true
                    Layout.minimumWidth: 200
                    Layout.maximumHeight: 60

                    onClicked: {
                        var role_index = roleCombo.currentIndex
                        if ( role_index < 0 )
                        {
                            error("请选择授权角色！")
                            return
                        }
                        grantDataset(m_uid, email.text, roleModel.get(role_index).role, m_from)
                    }
                }

                OrangeButton {
                    text: "返回"
                    //Layout.fillHeight: true
                    Layout.minimumWidth: 200
                    Layout.maximumHeight: 60
                    Layout.minimumHeight: 12*1.5

                    onClicked: {
                        cancel(m_from)
                    }
                }

                Rectangle {
                    color: "transparent"
                    Layout.fillWidth: true
                }
            }
            Rectangle {
                Layout.row: 4
                Layout.column: 1
                Layout.fillHeight: true
                Layout.fillWidth: true
                color: "transparent"
            }

        }
    }
}
